package logic

import (
	"box-mart-api/models/mysql"
	"context"
	"errors"

	"box-mart-api/boxrpc/boxrpc"
	"box-mart-api/boxrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type BoxUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBoxUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BoxUpdateLogic {
	return &BoxUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 修改
func (l *BoxUpdateLogic) BoxUpdate(in *boxrpc.BoxUpdateRequest) (*boxrpc.BoxUpdateResponse, error) {
	// todo: add your logic here and delete this line
	var box mysql.Box
	err := box.BoxDetail(mysql.DB, in.BoxId)
	if err != nil {
		return nil, errors.New("商品不存在,不允许修改")
	}
	box = mysql.Box{
		Model:    gorm.Model{ID: uint(in.BoxId)},
		BoxTitle: in.BoxTitle,
		BoxName:  in.BoxName,
		BoxPrice: in.BoxPrice,
		BoxType:  in.BoxType,
	}
	err = box.BoxUpdate(mysql.DB, in.BoxId)
	if err != nil {
		return nil, err
	}
	return &boxrpc.BoxUpdateResponse{
		BoxId:    int64(box.ID),
		BoxTitle: in.BoxTitle,
		BoxName:  in.BoxName,
		BoxPrice: in.BoxPrice,
		BoxType:  in.BoxType,
	}, nil
}
